#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer *t = new QTimer();
    connect(t,&QTimer::timeout,this,&MainWindow::getPose);
    t->start(1000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
 * @brief MainWindow::
 */
void MainWindow::on_search_clicked()
{
    device_number = ui->serial_number->text().toInt();
    int *comlist = getComPort();
    ui->info->append(QString::number(*comlist));
    ui->info->append("com :"+QString::number(device_number));
}

void MainWindow::on_open_clicked()
{
   int isOpen =   serialOperation(device_number,115200,true);
   m_isOpen = true;
   if(isOpen == 1){
       ui->info->append(tr("serial number %1 open serial sucessful!").arg(device_number));
   }else{
       ui->info->append(tr("serial number %1 open serial faill!").arg(device_number));

   }
}

void MainWindow::on_close_clicked()
{
    int isOpen  =   serialOperation(device_number,115200,false);
    m_isOpen = false;
    if(isOpen ==1){
        ui->info->append("close serial sucessful!");
    }else{
        ui->info->append(tr("close serial faill!").arg(isOpen));

    }
}

void MainWindow::on_ablen_clicked()
{
    int isOpen =   clawEnable(9,true);
    if(isOpen ==1){
        ui->info->append("enable sucessful!");
    }else{
        ui->info->append(tr("enable faill!").arg(isOpen));

    }
}

void MainWindow::on_disable_clicked()
{
    int isOpen =   clawEnable(9,false);
    if(isOpen ==1){
        ui->info->append("disenable sucessful!");
    }else{
        ui->info->append(tr("disenable faill!").arg(isOpen));

    }
}

void MainWindow::on_set_number_clicked()
{
    int cmd_number = ui->control_num->text().toInt();
    int isOpen =   runWithParam(9,cmd_number,255,255);
    if(isOpen ==1){
        ui->info->append(tr("set_number sucessful!").arg(cmd_number));
    }else{
        ui->info->append(tr("set_number %1 faill!").arg(cmd_number));

    }
}

void MainWindow::getPose()
{
    if(!m_isOpen){return;}
    int pose = getClawCurrentLocation(9);
    ui->info->append(tr("pose %1").arg(pose));
}

void MainWindow::on_disable_2_clicked()
{
    ui->info->clear();
}
